import React, { useEffect, useState, useImperativeHandle } from 'react'
import { message, Table, Button ,Popconfirm} from 'antd';
import { Action, AuthButton, TableCard } from 'components';
import { PlusOutlined } from '@ant-design/icons'
import { management_GetList, management_Del } from '../service'
const Management = ({ setIsModalOpen, setAddType }, ref) => {
    const [dataSource, setdataSource] = useState([])
    const [isFetching, setIsFetching] = useState(false);

    useEffect(() => {
        Management_GetData()
    }, [])

    const Management_GetData = () => {
       setIsFetching(true)
        management_GetList().then(res => {
          
            setdataSource(res.data)
            setIsFetching(false)
        })
    }

    useImperativeHandle(ref, () => ({
        Management_GetData
    }));
    return (
        <div>
            <TableCard
                rowKey="zone_number"
                dataSource={dataSource}
                pagination={false}
                loading={isFetching}
                onReload={Management_GetData}
                actions={[
                    <Button
                        type="primary"
                        icon={<PlusOutlined />}
                        onClick={() => {
                            console.log("点击")
                            setAddType("管理区分区")
                            setIsModalOpen(true)
                        }}
                    >新建</Button>
                ]}
            >
                <Table.Column title="管理区分区" dataIndex="area_name" />
                <Table.Column title="锁类型" dataIndex="lock_type" />

                <Table.Column width={80} title="操作" render={info => (
                    <Action>

                        <Popconfirm placement="top" title={`是否删除管理区${info.area_name}`} onConfirm={() => {
                            management_Del(info.id).then((res) => {
                                console.log(res)

                                if (res.code == 0) {
                                    message.success("删除成功")
                                   Management_GetData()
                                } else {
                                    message.error("删除失败")
                                }

                            })
                        }} okText="删除" cancelText="取消">
                            <Action.Button danger


                            >删除</Action.Button>
                        </Popconfirm>
       
                    </Action>
                )} />
            </TableCard>
        </div>
    )
}

export default React.forwardRef(Management)